package com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.orderservice.service.impl;

import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.orderservice.service.OrderService;
import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.orderservice.service.OrderTccService;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 订单 service实现类
 *
 * @author ziyucoding
 * @date 2022-11-04
 **/
@Service
public class OrderServiceImpl implements OrderService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderTccService orderTccService;

    @Override
    @GlobalTransactional // 声明全局事务
    public Integer createOrder(Long userId, Long productId, Integer price, Integer amount) throws Exception {
        // todo 哦全局发号器生成订单唯一ID
        int orderId = ((Long) System.currentTimeMillis()).intValue();
        orderTccService.tryCreate(null, userId, orderId, productId, price, amount);
        // 返回订单编号
        return orderId;
    }

}
